#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //实例化四个界面
    loginWig = new Login;
    firstpageWig = new FirstPage;
    modbusWig = new ModbusWig;
    stmWig = new Stm32;
    controlWig = new Control;
    hisWig = new History;
    setWig = new SetWig;


    //将七个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(loginWig);
    ui->stackedWidget->addWidget(firstpageWig);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(stmWig);
    ui->stackedWidget->addWidget(controlWig);
    ui->stackedWidget->addWidget(hisWig);
    ui->stackedWidget->addWidget(setWig);


    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.50.157"));
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     modbusWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     loginWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     firstpageWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     stmWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     controlWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     hisWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     setWig, SLOT(dataUpSlot(QByteArray)));

}

HomePage::~HomePage()
{
    delete ui;
}







